package GUI;

import java.awt.Dimension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import org.jdesktop.layout.GroupLayout;

import GUI.manager.VentanaFactory;
import GUI.manager.VentanaManager;



public class panelInicio extends javax.swing.JPanel implements ActionListener {

	private static final int ERROR_MESSAGE = 0;
	private static final int OK_CANCEL_OPTION = 0;
	protected static final int PLAIN_MESSAGE = 0;
	public static String nick;
	

	public panelInicio() {
		initComponents();
	}

	private void initComponents() {

		etiquetaTitulo = new javax.swing.JLabel();
		botonRegistro = new javax.swing.JButton();
		etiquetaUsuario = new javax.swing.JLabel();
		etiquetaContrasenia = new javax.swing.JLabel();
		textoUsuario = new javax.swing.JTextField();
		botonRecordar = new javax.swing.JButton();
		textoContrasenia = new javax.swing.JPasswordField();
		botonAceptar = new javax.swing.JButton();
		botonAceptar1 = new javax.swing.JButton();
		botonSalir = new javax.swing.JButton();

		setBackground(new java.awt.Color(255, 255, 255));
		
		
		this.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent evt){
				if(evt.getKeyCode()==KeyEvent.VK_ENTER){
					Icon icono = new ImageIcon("src/GUI/resources/error.gif");
					panelPrincipal.nick = textoUsuario.getText();
					System.out.println(panelPrincipal.nick);

					if ((textoUsuario.getText().length() == 0)
							&& (textoContrasenia.getText().length() > 0)) {

						JOptionPane.showOptionDialog(botonAceptar,
								"No has introducido tu nombre de usuario",
								"Error login", OK_CANCEL_OPTION, ERROR_MESSAGE, icono,
								new Object[] { "ACEPTAR" }, null);
						etiquetaUsuario.setForeground(new java.awt.Color(255, 0, 0));
						etiquetaUsuario.setText("USUARIO*");
						etiquetaContrasenia.setForeground(new java.awt.Color(102, 102,
								102));
						etiquetaContrasenia.setText("PASSWORD");

					} else {
						if ((textoContrasenia.getText().length() == 0)
								&& (textoUsuario.getText().length() > 0)) {
							JOptionPane.showOptionDialog(botonAceptar,
									"No has introducido la password", "Error login",
									OK_CANCEL_OPTION, ERROR_MESSAGE, icono,
									new Object[] { "ACEPTAR" }, null);
							etiquetaContrasenia.setForeground(new java.awt.Color(255,
									0, 0));
							etiquetaContrasenia.setText("PASSWORD*");
							etiquetaUsuario.setForeground(new java.awt.Color(102, 102,
									102));
							etiquetaUsuario.setText("USUARIO");

						} else {
							if ((textoContrasenia.getText().length() == 0)
									&& (textoUsuario.getText().length() == 0)) {
								JOptionPane
										.showOptionDialog(
												botonAceptar,
												"No has introducido el nombre de usuario ni la password",
												"Error login", OK_CANCEL_OPTION,
												ERROR_MESSAGE, icono,
												new Object[] { "ACEPTAR" }, null);
								etiquetaUsuario.setForeground(new java.awt.Color(255,
										0, 0));
								etiquetaUsuario.setText("USUARIO*");
								etiquetaContrasenia.setForeground(new java.awt.Color(
										255, 0, 0));
								etiquetaContrasenia.setText("PASSWORD*");
							} else {
								if (textoUsuario.getText().equals("administrador")
										&& (textoContrasenia.getText()
												.equals("laboratorio50"))) {
									actual = (JFrame) UtilidadesGUI
											.getContenedorPrincipal(estePanel);
									actual.getContentPane().remove(0);
									actual.getContentPane().add(
											new panelAdministradorPrincipal());
									actual.setPreferredSize(new Dimension(850, 630));
									actual.pack();
									actual.repaint();
									UtilidadesGUI.CentrarPantalla(actual);
								} else {
								
									/*VentanaManager ventanaManager= VentanaFactory.getInstance(); REVISAR
									HashMap<String, String> hashmap= ventanaManager.getPasswords();
									if(hashmap.get(textoUsuario.getText()).equals(null))
									{
										Icon icono2 = new ImageIcon("src/GUI/resources/error.gif");
					
					
						
										JOptionPane.showOptionDialog(null, "El usuario o la password no coinciden",
										"Login incorrecto", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono2,
										new Object[] { "ACEPTAR" }, null);
										etiquetaUsuario.setText("USUARIO*");
										etiquetaContrasenia.setForeground(new java.awt.Color(255, 0, 0));
										etiquetaContrasenia.setForeground(new java.awt.Color(255,0, 0));
										etiquetaContrasenia.setText("PASSWORD*");
									}*/
						
									}
									actual = (JFrame) UtilidadesGUI
											.getContenedorPrincipal(estePanel);
									actual.getContentPane().remove(0);
									actual.getContentPane().add(new panelPrincipal());
									actual.setPreferredSize(new Dimension(850, 630));
									actual.pack();
									actual.repaint();
									UtilidadesGUI.CentrarPantalla(actual);

								}
							}

						}

					}
				}
				
			
		});
		this.requestFocus();
		
	

		textoUsuario.setFont(new java.awt.Font("Calibri", 0, 13));
	
		textoContrasenia.setFont(new java.awt.Font("Calibri", 0, 13));
		
		
		etiquetaTitulo.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/encabezadoMain.png")); // NOI18N

		botonRegistro.setBackground(new java.awt.Color(255, 255, 255));
		botonRegistro.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		botonRegistro.setForeground(new java.awt.Color(243, 15, 120));
		botonRegistro.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonRegistro.png")); // NOI18N
		botonRegistro.setBorder(null);
		botonRegistro.addActionListener(this);

		etiquetaUsuario.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		etiquetaUsuario.setForeground(new java.awt.Color(102, 102, 102));
		etiquetaUsuario.setText("USUARIO");

		etiquetaContrasenia.setFont(new java.awt.Font("Lucida Handwriting", 1,
				14)); // NOI18N
		etiquetaContrasenia.setForeground(new java.awt.Color(102, 102, 102));
		etiquetaContrasenia.setText("PASSWORD");

		botonRecordar.setBackground(new java.awt.Color(255, 255, 255));
		botonRecordar.setFont(new java.awt.Font("Lucida Handwriting", 1, 10)); // NOI18N
		botonRecordar.setForeground(new java.awt.Color(102, 102, 102));
		botonRecordar.setText("RECORDAR");
		botonRecordar.setBorder(null);
		botonRecordar.addActionListener(this);

		botonAceptar.setBackground(new java.awt.Color(255, 255, 255));
		botonAceptar.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		botonAceptar.setForeground(new java.awt.Color(243, 15, 120));
		botonAceptar.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botoneraAceptar.png"));
		botonAceptar.setBorder(null);
		botonAceptar.addActionListener(this);
		

		botonAceptar1.setBackground(new java.awt.Color(255, 255, 255));
		botonAceptar1.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		botonAceptar1.setForeground(new java.awt.Color(243, 15, 120));
		botonAceptar1.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botoneraAceptar.png"));
		botonAceptar1.setBorder(null);
		// botonAceptar1.addActionListener(this);

		botonSalir.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/menu-salir.png")); // NOI18N
		botonSalir.setBorderPainted(false);
		botonSalir.addActionListener(this);
		botonSalir.setBackground(new java.awt.Color(255, 255, 255));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout
										.createSequentialGroup()
										.addContainerGap()
										.add(etiquetaTitulo,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE))
								.add(layout
										.createSequentialGroup()
										.add(101, 101, 101)
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.TRAILING)
												.add(etiquetaUsuario)
												.add(etiquetaContrasenia))
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.LEADING)
												.add(layout
														.createSequentialGroup()
														.add(18, 18, 18)
														.add(layout
																.createParallelGroup(
																		org.jdesktop.layout.GroupLayout.LEADING)
																.add(textoUsuario,
																		org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																		201,
																		org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																.add(layout
																		.createSequentialGroup()
																		.add(textoContrasenia,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				201,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(botonRecordar))))
												.add(layout
														.createSequentialGroup()
														.add(32, 32, 32)
														.add(botonAceptar)))
										.add(0, 0, Short.MAX_VALUE)))
						.addContainerGap())
				.add(org.jdesktop.layout.GroupLayout.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.add(botonSalir)
								.addPreferredGap(
										org.jdesktop.layout.LayoutStyle.RELATED,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE).add(botonRegistro)
								.add(22, 22, 22)));
		layout.setVerticalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.add(12, 12, 12)
						.add(etiquetaTitulo,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								108,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.UNRELATED)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(textoUsuario,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.add(etiquetaUsuario))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(etiquetaContrasenia)
								.add(textoContrasenia,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.add(botonRecordar))
						.add(18, 18, 18)
						.add(botonAceptar,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								44,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.add(6, 6, 6)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout
										.createSequentialGroup()
										.add(botonRegistro,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												44,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(14, 14, 14))
								.add(org.jdesktop.layout.GroupLayout.TRAILING,
										layout.createSequentialGroup()
												.add(botonSalir)
												.addContainerGap()))));
	}

	private javax.swing.JButton botonAceptar;
	private javax.swing.JButton botonAceptar1;

	private javax.swing.JButton botonRecordar;
	private javax.swing.JButton botonRegistro;
	private javax.swing.JButton botonSalir;
	private javax.swing.JLabel etiquetaContrasenia;
	private javax.swing.JLabel etiquetaTitulo;
	private javax.swing.JLabel etiquetaUsuario;
	private javax.swing.JPasswordField textoContrasenia;
	private javax.swing.JTextField textoUsuario;
	private JFrame actual;
	private JPanel estePanel = this;
	
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == botonRegistro) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelRegistro());
			actual.setPreferredSize(new Dimension(610, 650));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);
		}

		if (e.getSource() == botonRecordar) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelRecordarPassword());
			actual.setPreferredSize(new Dimension(610, 330));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		

		if (e.getSource() == botonAceptar) {
			Icon icono = new ImageIcon("src/GUI/resources/error.gif");
			panelPrincipal.nick = textoUsuario.getText();
			System.out.println(panelPrincipal.nick);

			if ((textoUsuario.getText().length() == 0)
					&& (textoContrasenia.getText().length() > 0)) {

				JOptionPane.showOptionDialog(botonAceptar,
						"No has introducido tu nombre de usuario",
						"Error login", OK_CANCEL_OPTION, ERROR_MESSAGE, icono,
						new Object[] { "ACEPTAR" }, null);
				etiquetaUsuario.setForeground(new java.awt.Color(255, 0, 0));
				etiquetaUsuario.setText("USUARIO*");
				etiquetaContrasenia.setForeground(new java.awt.Color(102, 102,
						102));
				etiquetaContrasenia.setText("PASSWORD");

			} else {
				if ((textoContrasenia.getText().length() == 0)
						&& (textoUsuario.getText().length() > 0)) {
					JOptionPane.showOptionDialog(botonAceptar,
							"No has introducido la password", "Error login",
							OK_CANCEL_OPTION, ERROR_MESSAGE, icono,
							new Object[] { "ACEPTAR" }, null);
					etiquetaContrasenia.setForeground(new java.awt.Color(255,
							0, 0));
					etiquetaContrasenia.setText("PASSWORD*");
					etiquetaUsuario.setForeground(new java.awt.Color(102, 102,
							102));
					etiquetaUsuario.setText("USUARIO");

				} else {
					if ((textoContrasenia.getText().length() == 0)
							&& (textoUsuario.getText().length() == 0)) {
						JOptionPane
								.showOptionDialog(
										botonAceptar,
										"No has introducido el nombre de usuario ni la password",
										"Error login", OK_CANCEL_OPTION,
										ERROR_MESSAGE, icono,
										new Object[] { "ACEPTAR" }, null);
						etiquetaUsuario.setForeground(new java.awt.Color(255,
								0, 0));
						etiquetaUsuario.setText("USUARIO*");
						etiquetaContrasenia.setForeground(new java.awt.Color(
								255, 0, 0));
						etiquetaContrasenia.setText("PASSWORD*");
					} else {
						if (textoUsuario.getText().equals("administrador")
								&& (textoContrasenia.getText()
										.equals("laboratorio50"))) {
							actual = (JFrame) UtilidadesGUI
									.getContenedorPrincipal(estePanel);
							actual.getContentPane().remove(0);
							actual.getContentPane().add(
									new panelAdministradorPrincipal());
							actual.setPreferredSize(new Dimension(850, 630));
							actual.pack();
							actual.repaint();
							UtilidadesGUI.CentrarPantalla(actual);
						} else {
						
							VentanaManager ventanaManager= VentanaFactory.getInstance();
							HashMap<String, String> hashmap= ventanaManager.getPasswords();
							if(hashmap.containsKey(textoUsuario.getText())==false)
							{
								 
								JOptionPane.showOptionDialog(null, "El usuario o la password son incorrectos",
								"Login incorrecto", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono,
								new Object[] { "ACEPTAR" }, null);
								etiquetaUsuario.setText("USUARIO*");
								etiquetaUsuario.setForeground(new java.awt.Color(255, 0, 0));
								etiquetaContrasenia.setForeground(new java.awt.Color(255,0, 0));
								etiquetaContrasenia.setText("PASSWORD*");
							}else{
								
								
								
								if(hashmap.get(textoUsuario.getText()).equals(textoContrasenia.getText())){
									
									Icon icono1 = new ImageIcon("src/GUI/resources/aceptar.gif");	
								JOptionPane.showOptionDialog(null, "Login Correcto",
								"Login incorrecto", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono1,
										new Object[] { "ACEPTAR" }, null);
								actual = (JFrame) UtilidadesGUI
										.getContenedorPrincipal(estePanel);
								actual.getContentPane().remove(0);
								actual.getContentPane().add(new panelPrincipal());
								actual.setPreferredSize(new Dimension(850, 630));
								actual.pack();
								actual.repaint();
								UtilidadesGUI.CentrarPantalla(actual);
								}else{
									JOptionPane.showOptionDialog(null, "El usuario o la password son incorrectos",
											"Login incorrecto", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono,
											new Object[] { "ACEPTAR" }, null);
											etiquetaUsuario.setText("USUARIO*");
											etiquetaUsuario.setForeground(new java.awt.Color(255, 0, 0));
											etiquetaContrasenia.setForeground(new java.awt.Color(255,0, 0));
											etiquetaContrasenia.setText("PASSWORD*");
									
								}
							}
							

						}
					}

				}

			
		

		

		
			}}
	if (e.getSource() == botonSalir) {
		actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
		actual.getContentPane().remove(0);
		actual.getContentPane().add(new panelSalir(1));
		actual.setPreferredSize(new Dimension(610, 330));
		actual.pack();
		actual.repaint();
		UtilidadesGUI.CentrarPantalla(actual);
		}}

}